//
//  SSHover.m
//  snomPhone
//
//  Created by Daniel Hazelbaker on 3/30/09.
//  Copyright 2009 Blue Box Moon. All rights reserved.
//

#import "SSHover.h"
#import "overlayWindow.h"
#import "overlayView.h"
#import "BMPluginController.h"


NSString *kHoverPanelImageKey = @"kHoverPanelImageKey";
NSString *kHoverPanelTitleKey = @"kHoverPanelTitleKey";
NSString *kHoverPanelMessageKey = @"kHoverPanelMessageKey";
NSString *kHoverPanelButtonsKey = @"kHoverPanelButtonsKey";


@implementation SSHover

@synthesize title, message, image, buttons, visibleTime, context, delegate;


- (id)initWithContent:(NSDictionary *)content notifyDelegate:(id)inDelegate
{
    if ([super initWithWindowNibName:@"hover"] == nil)
	return nil;

    delegate = inDelegate;
    self.title = [content valueForKey:kHoverPanelTitleKey];
    self.message = [content valueForKey:kHoverPanelMessageKey];
    self.image = [content valueForKey:kHoverPanelImageKey];
    self.buttons = [content valueForKey:kHoverPanelButtonsKey];
    self.visibleTime = 5;
    
    return self;
}


//
// Perform all the layout now that we are initialized.
//
- (void)awakeFromNib
{
    NSRect  rc;
    NSSize  sz;
    float   heightDiff;
    

    //
    // Determine size needed to fit the message.
    //
    rc = messageView.frame;
    sz = [[messageView cell] cellSizeForBounds:NSMakeRect(0, 0, rc.size.width, 10000.0f)];
    heightDiff = (sz.height - rc.size.height);
    
    //
    // Resize the window to fit properly.
    //
    if (heightDiff > 0)
    {
	rc = [[self window] frame];
	rc.size.height += heightDiff;
	[[self window] setFrame:rc display:NO];
    }
    else
    {
	rc.origin.y -= heightDiff;
	rc.size.height += heightDiff;
	messageView.frame = rc;
    }
    
    //
    // If there are buttons, add them in.
    //
    if ([buttons count] > 0)
    {
	NSMutableArray	*buttonViews = [NSMutableArray array];
	NSButton	*button;
	NSView		*contentView;
	int		i, maxButtonHeight = 24, x;
	
	
	contentView = [[self window] contentView];
	
	for (i = 0; i < [buttons count]; i++)
	{
	    if ([[buttons objectAtIndex:i] isKindOfClass:[NSImage class]])
	    {
		NSImage *img = [buttons objectAtIndex:i];
		
		//
		// Generate a button based on a string value.
		//
		button = [[[NSButton alloc] init] autorelease];
		[button setImage:img];
		[button setBordered:NO];
		[button setButtonType:NSMomentaryChangeButton];
		[button setTag:i];
		[button setTarget:self];
		[button setAction:@selector(hoverButtonClicked:)];
		rc = [button frame];
		rc.size = img.size;
		[button setFrame:rc];
		
		[buttonViews addObject:button];
		maxButtonHeight = (img.size.height > maxButtonHeight ? img.size.height : maxButtonHeight);
	    }
	    else
	    {
		//
		// Generate a button based on a string value.
		//
		button = [[[NSButton alloc] init] autorelease];
		[button setTitle:[buttons objectAtIndex:i]];
		[button setBezelStyle:NSTexturedRoundedBezelStyle];
		[button sizeToFit];
		[button setTag:i];
		[button setTarget:self];
		[button setAction:@selector(hoverButtonClicked:)];
		rc = [button frame];
		rc.size.height = 24;
		[button setFrame:rc];
		
		[buttonViews addObject:button];
	    }
	}

	//
	// Resize the window.
	//
	int resizeAmount;
	if (heightDiff < 0)
	{
	    resizeAmount = (maxButtonHeight + heightDiff);
	    if (resizeAmount < 0)
		resizeAmount = 0;
	}
	else
	    resizeAmount = maxButtonHeight;
	rc = [[self window] frame];
	rc.size.height += resizeAmount + 8;
	[[[self window] contentView] setAutoresizesSubviews:NO];
	[[self window] setFrame:rc display:NO];
	[[[self window] contentView] setAutoresizesSubviews:YES];

	//
	// Reposition the image.
	//
	rc = imageView.frame;
	rc.origin.y += resizeAmount + 8;
	imageView.frame = rc;

	//
	// Reposition the message.
	//
	rc = messageView.frame;
	rc.origin.y += resizeAmount + 8;
	messageView.frame = rc;

	//
	// Now place all the buttons.
	//
	x = contentView.frame.size.width - 8;
	for (i = 0; i < [buttonViews count]; i++)
	{
	    button = [buttonViews objectAtIndex:i];
	    rc = button.frame;
	    rc.origin.y = floorf((maxButtonHeight - rc.size.height) / 2.0f) + 8.0f;
	    rc.origin.x = (x - rc.size.width);
	    button.frame = rc;
	    x -= (rc.size.width + 8);
	    
	    [contentView addSubview:button];
	}
    }
}


- (void)show
{
    overlayWindow   *window;
    

    [self retain];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDismissed:) name:BMOverlayWindowDidDeactivate object:[self window]];
    window = (overlayWindow *)[self window];
    [(overlayView *)[window contentView] setValue:[NSColor colorWithCalibratedWhite:0.7f alpha:0.9f] forKey:@"backgroundColor"];
    [window setVisibleTime:visibleTime];
    [window setPlacement:BMOverlayPlacementTopTight];
    [window activate:self];
}


- (void)dismiss
{
    [(overlayWindow *)[self window] deactivate:self];
}


//
// The window has been dismissed, release ourselves.
//
- (void)windowDismissed:(NSNotification *)notification
{
    if ([delegate respondsToSelector:@selector(popupNotificationDidDismiss:)])
	[delegate popupNotificationDidDismiss:self];

    [self autorelease];
}


- (void)hoverButtonClicked:(NSButton *)sender
{
    if (delegate && [delegate respondsToSelector:@selector(popupNotification:didClickButton:)])
	[delegate popupNotification:self didClickButton:[sender tag]];
}


@end
